package org.data12.SpringbootDemo.controller;

import org.data12.SpringbootDemo.model.Admin;
import org.data12.SpringbootDemo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/loginCheck")
    public String loginCheck(Model model, String username, String password, HttpSession session){
        Admin admin = adminService.login(username, password);
        if(admin!= null){
            session.setAttribute("uid", admin.getId());
            return "redirect:/student/list";
        }else {
            model.addAttribute("msg", "用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("uid");
        return "redirect:/login";
    }
}
